﻿using System;
using eCommerce.db;
using eCommerce.db.BORespository;
using System.Web.UI.WebControls;
using eCommerce.core.Helpers;
using System.Configuration;
using eCommerce.core;

namespace eCommerce.web.Controls.Home
{
    public partial class Discount : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                ListDiscount();
        }

        private void ListDiscount()
        {
            var lstProduct = ProductBo.GetListDiscount(Constant.TopDiscount);
            if (lstProduct.Count > 0)
            {
                rptProduct.DataSource = lstProduct;
                rptProduct.DataBind();
            }
        }

        protected void rptProduct_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var ltCategory = (Literal)e.Item.FindControl("ltCategory");
                var categoryId = Utils.ToInt32(((HiddenField)e.Item.FindControl("hfCategoryId")).Value);
                Literal ltrHref = (Literal)e.Item.FindControl("ltrHref");
                Literal ltrHrefBelow = (Literal)e.Item.FindControl("ltrHrefBelow");
                var ltImage = ((Literal)e.Item.FindControl("ltImage"));
                var status = ((HiddenField)e.Item.FindControl("hdStatus")).Value;
                var productId = Utils.ToInt32(((HiddenField)e.Item.FindControl("hfProductId")).Value);
                Literal ltrIconNew = (Literal)e.Item.FindControl("ltrIconNew");

                var name = Utils.UnsignAndDash(((HiddenField)e.Item.FindControl("hfName")).Value);
                var imagePath = ((HiddenField)e.Item.FindControl("hdImagePath")).Value;

                var cate = CategoryBo.GetById(categoryId);
                if (cate.Url.ToString().Contains("/"))
                {
                    string[] arr = cate.Url.ToString().Split('/');
                    cate.Url = arr[0];
                }

                ltrHref.Text = ltrHrefBelow.Text = "<a href=\"" + ConfigurationManager.AppSettings["Domain"] + UrlHelper.ProductDetail(cate.Url, productId, name) + " \">";

                string Img1 = ConfigurationManager.AppSettings["Thumbnail"] + string.Format("/Thumb.ashx?imgPath={0}&tid={1}&size=200", imagePath, (int)SettingType.Product);
                ltImage.Text = "<img src=\"" + Img1 + "\" />";

         
            }
        }
    }
}